﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using System.IO;
using System.Globalization;


namespace ImPM.Web.Services
{
    public abstract class SoapExtensionExceptionHandlerBase : ISoapExtensionExceptionHandler
    {
        // Methods
        protected SoapExtensionExceptionHandlerBase()
        {
        }

        protected CrmException GetCrmException(Exception exception)
        {
            CrmException exception2 = exception as CrmException;
            if (exception2 != null)
            {
                return exception2;
            }
            COMException innerException = exception as COMException;
            if (innerException != null)
            {
                return new CrmException(string.Empty, innerException, innerException.ErrorCode);
            }
            UnauthorizedAccessException exception4 = exception as UnauthorizedAccessException;
            if (exception4 != null)
            {
                return new CrmException(string.Empty, exception4, -2147187707);
            }
            SqlException exception5 = exception as SqlException;
            if (exception5 != null)
            {
                return new CrmException(string.Empty, exception5, -2147204784);
            }
            if (exception.InnerException != null)
            {
                return this.GetCrmException(exception.InnerException);
            }
            return null;
        }

        public abstract bool Handle(Stream to, Stream from, Exception exception);
        protected void WriteSoapFault(Stream to, Stream from, Exception exception, int errorCode, string description)
        {
            TextWriter w = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter writer2 = new XmlTextWriter(w);
            writer2.Formatting = Formatting.Indented;
            writer2.WriteStartElement("error", string.Empty);
            string strA = errorCode.ToString("x", CultureInfo.InvariantCulture);
            writer2.WriteElementString("code", "0x" + strA);
            writer2.WriteElementString("description", description);
            writer2.WriteElementString("type", "Platform");
            if ((exception.Data != null) && (exception.Data.Count > 0))
            {
                writer2.WriteStartElement("data");
                foreach (object obj2 in exception.Data.Keys)
                {
                    writer2.WriteStartElement("pair");
                    writer2.WriteElementString("key", obj2.ToString());
                    writer2.WriteElementString("value", exception.Data[obj2].ToString());
                    writer2.WriteEndElement();
                }
                writer2.WriteEndElement();
            }
            if ((((int)RegistryCache.GetValue("DebugExceptionInfo", 0)) == 1) || (string.Compare(strA, "80048000", StringComparison.OrdinalIgnoreCase) == 0))
            {
                StringBuilder builder = new StringBuilder();
                for (Exception exception2 = exception; exception2 != null; exception2 = exception2.InnerException)
                {
                    builder.AppendFormat("{0}{1}", exception2.Message, Environment.NewLine);
                    builder.AppendFormat("{0}{1}{1}", exception2.StackTrace, Environment.NewLine);
                    SoapException exception3 = exception2 as SoapException;
                    if (exception3 != null)
                    {
                        if (exception3.Detail != null)
                        {
                            builder.AppendFormat("{0}{1}", exception3.Detail.InnerXml, Environment.NewLine);
                        }
                    }
                    else
                    {
                        builder.AppendFormat("InnerException Type:{0}{1}", exception2.GetType().FullName, Environment.NewLine);
                    }
                }
                writer2.WriteElementString("details", builder.ToString());
            }
            writer2.WriteEndElement();
            writer2.Flush();
            XmlDocument document = new XmlDocument();
            document.Load(from);
            XmlDocumentFragment newChild = document.CreateDocumentFragment();
            newChild.InnerXml = w.ToString();
            XmlNode node = document.GetElementsByTagName("detail")[0];
            if (node != null)
            {
                node.RemoveAll();
            }
            else
            {
                node = document.CreateElement("detail");
                document.GetElementsByTagName("Fault")[0].AppendChild(node);
            }
            node.AppendChild(newChild);
            XmlNode node3 = document.GetElementsByTagName("faultstring")[0];
            node3.RemoveAll();
            node3.InnerText = "Server was unable to process request.";
            XmlWriter writer3 = new XmlTextWriter(to, Encoding.UTF8);
            document.WriteTo(writer3);
            writer3.Flush();
        }
    }


}
